package com.juzipi.demo.ocp;

public class Ocp {

    public static void main(String[] args) {
        //使用看看存在的问题
        GraphicEditor graphicEditor = new GraphicEditor();
        graphicEditor.drawShape(new Rectangle());
        graphicEditor.drawShape(new Circle());
        graphicEditor.drawTriangle(new Triangle());
    }

}

//这是一个用于绘图的类
//这是使用方
class GraphicEditor{
    //接收Shape对象，然后根据type，来绘制不同的图形
    public void drawShape(Shape shape){
        if (shape.my_type == 1){
            drawRectangle(shape);
        }
        if (shape.my_type == 2){
            drawCircle(shape);
        }
        if (shape.my_type == 3){
            drawTriangle(shape);
        }
    }

    //绘制矩形
    public void drawRectangle(Shape shape){
        System.out.println("绘制了矩形");
    }

    //绘制圆形
    public void drawCircle(Shape shape){
        System.out.println("绘制了圆形");
    }

    //绘制三角形
    public void drawTriangle(Shape shape){
        System.out.println("绘制了三角形");
    }

}

//Shaper基类
class Shape{
    int my_type;
}

class Rectangle extends Shape {
    Rectangle(){
        super.my_type = 1;
    }
}

class Circle extends Shape {
    Circle() {
        super.my_type = 2;
    }
}

//新增画三角形
class Triangle extends Shape {
    Triangle(){
        super.my_type = 3;
    }
}